package smb.interceptor;

import java.util.Arrays;

import org.apache.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;

import entity.User;

public class UserLogger {
	private static final Logger Log = Logger.getLogger(UserLogger.class);

	public void before(JoinPoint jp) {
		Log.warn("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
		System.out.println("在" + new java.util.Date() + "调用了"
				+ jp.getSignature().getName() + ",参数是"
				+ Arrays.toString(jp.getArgs()));
		if (jp.getSignature().getName().equals("reg")) {
			User user = (User) jp.getArgs()[0];
			if (user.getUsername() == null || user.getUsername().length() < 6) {
				// System.out.println("参数错误！");
				throw new RuntimeException("参数错误！");
			}
		}
		System.out.println("***********************************");
	}

	public void after(JoinPoint jp) {
		Log.info("在" + new java.util.Date() + "调用了"
				+ jp.getSignature().getName() + "方法 执行结束！");

	}

	public Object huanrao(ProceedingJoinPoint jp) {
		Object r = null;
		long start = System.nanoTime();
		try {
			Log.info("在" + new java.util.Date() + "调用了"
					+ jp.getSignature().getName() + ",参数是"
					+ Arrays.toString(jp.getArgs()));
			r = jp.proceed();// 放行！
			Log.info("在" + new java.util.Date() + "调用了"
					+ jp.getSignature().getName() + "方法 执行结束！\n返回值是" + r);
		} catch (Throwable e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
		long end = System.nanoTime();
		Log.info(jp.getSignature().getName() + "执行了" + (end - start) + "毫微秒！");
		return r;
	}
}
